﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SQLCheck
{
    public partial class FileShow : Form
    {
        string FilePath = "";
        Dictionary<string, List<string>> reg = new Dictionary<string, List<string>>();
        public FileShow(string pathFile, Dictionary<string, List<string>> result)
        {
            InitializeComponent();
            this.FilePath = pathFile;
            this.reg = result;
        }

        List<int> indexAll = new List<int>();
        int index = 0;

        private void FileShow_Load(object sender, EventArgs e)
        {
            this.Text = FilePath;
            rtbTxt.Text = File.ReadAllText(FilePath);
            int error = 0;
            foreach (var item in reg.Keys)
            {
                string str = item.Replace("\r", "");
                var lastIndex = rtbTxt.Text.IndexOf(str);
                indexAll.Add(lastIndex);
                //var lastIndex = rtbTxt.Find(str);
                if (lastIndex == -1)
                {
                    error++;
                    continue;
                }
                rtbTxt.Select(lastIndex, str.Length);
                rtbTxt.SelectionColor = Color.Red;

                foreach (var value in reg[item])
                {
                    var lindex = rtbTxt.Find(value, lastIndex, lastIndex + str.Length, RichTextBoxFinds.None);
                    if (lindex != -1)
                    {
                        rtbTxt.Select(lindex, value.Length);
                        rtbTxt.SelectionColor = Color.Blue;
                    }
                }

            }
            if (indexAll.Count > 0)
            {
                indexAll = indexAll.Distinct().ToList();
                index = 0;
                SetInfo();
            }
            else
            {
                panel1.Visible = false;
            }
            if (error > 0)
                MessageBox.Show($"初始化边色发送【{error}】次错误");

        }

        private void btnNext_Click(object sender, EventArgs e)
        {

        }

        private void SetInfo()
        {
            rtbTxt.Select(indexAll[index], 6);
            rtbTxt.ScrollToCaret();
            lbmsg.Text = $"{index + 1}/{indexAll.Count}";
        }

        private void rtbTxt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F3)
            {
                index++;
                if (index >= indexAll.Count)
                {
                    index = 0;
                }
                SetInfo();
            }
            if (e.Modifiers == Keys.Control)
            {
                switch (e.KeyCode)
                {
                    case Keys.S://cril+s
                        MessageBox.Show("还没有哟");
                        break;
                }

            }
        }
    }
}
